{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "dcc3abfa",
   "metadata": {},
   "outputs": [],
   "source": [
    "def calXnew(Xold, arr_A, vec_b):\n",
    "    return A @ Xold + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "1495f9c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "cf39b397",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.array([\n",
    "    [0, - 2/ 3],\n",
    "    [- 1/ 3, 0]\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "200df162",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.        , -0.66666667],\n",
       "       [-0.33333333,  0.        ]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "4083da76",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.array([2, 1/ 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "e70efbcd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2.        , 0.33333333])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "2def0f6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "Xold = np.array([1, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "4a6e9382",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.33333333, 0.        ])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "calXnew(Xold, A, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "3b68e895",
   "metadata": {},
   "outputs": [],
   "source": [
    "Xnew = calXnew(Xold, A, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "93715a44",
   "metadata": {},
   "outputs": [],
   "source": [
    "Xold = Xnew\n",
    "Xnew = calXnew(Xold, A, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "28a1f50e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2.        , -0.11111111])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Xnew"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "2235f101",
   "metadata": {},
   "outputs": [],
   "source": [
    "Xold = np.array([1, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "b1bfd1b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 [ 2.28519817 -0.42833918]\n",
      "1 [ 2.28555945 -0.42839939]\n",
      "2 [ 2.28559959 -0.42851982]\n",
      "3 [ 2.28567988 -0.4285332 ]\n",
      "4 [ 2.2856888  -0.42855996]\n",
      "5 [ 2.28570664 -0.42856293]\n",
      "6 [ 2.28570862 -0.42856888]\n",
      "7 [ 2.28571259 -0.42856954]\n",
      "8 [ 2.28571303 -0.42857086]\n",
      "9 [ 2.28571391 -0.42857101]\n",
      "10 [ 2.28571401 -0.4285713 ]\n",
      "11 [ 2.2857142  -0.42857134]\n",
      "12 [ 2.28571422 -0.4285714 ]\n",
      "13 [ 2.28571427 -0.42857141]\n",
      "14 [ 2.28571427 -0.42857142]\n",
      "15 [ 2.28571428 -0.42857142]\n",
      "16 [ 2.28571428 -0.42857143]\n",
      "17 [ 2.28571428 -0.42857143]\n",
      "18 [ 2.28571429 -0.42857143]\n",
      "19 [ 2.28571429 -0.42857143]\n"
     ]
    }
   ],
   "source": [
    "for i in range(20):\n",
    "    Xnew = calXnew(Xold, A, b)\n",
    "    Xold = Xnew\n",
    "    print(i, Xnew)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "cc21e581",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2.28571429, -0.42857143])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linalg.inv([[3, 2],[1, 3]]) @ np.array([6, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "879f7089",
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 4\n",
    "A = np.random.randn(n, n)\n",
    "b = np.random.randn(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "1335a27f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.07454441,  0.63082665,  0.06553787,  0.66237282],\n",
       "       [-0.92731433, -0.22661526, -0.42374286, -1.69840544],\n",
       "       [ 0.31146032,  0.34015829, -1.46601756, -0.03633514],\n",
       "       [ 1.29970226,  1.43332141, -0.61407238,  0.05173542]])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "63e111a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.14866   , -0.05630022, -0.3259487 ,  0.31873426])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "04538774",
   "metadata": {},
   "outputs": [],
   "source": [
    "diag = np.diag(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "403f13b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "D = np.diag(diag)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "bcf86950",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.07454441,  0.        ,  0.        ,  0.        ],\n",
       "       [ 0.        , -0.22661526,  0.        ,  0.        ],\n",
       "       [ 0.        ,  0.        , -1.46601756,  0.        ],\n",
       "       [ 0.        ,  0.        ,  0.        ,  0.05173542]])"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "a2ceb0ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "L = np.tril(A, -1) #得到下三角矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "9b75562c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.        ,  0.        ,  0.        ,  0.        ],\n",
       "       [-0.92731433,  0.        ,  0.        ,  0.        ],\n",
       "       [ 0.31146032,  0.34015829,  0.        ,  0.        ],\n",
       "       [ 1.29970226,  1.43332141, -0.61407238,  0.        ]])"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "71295b5d",
   "metadata": {},
   "outputs": [],
   "source": [
    "U = np.triu(A, 1) #上三角"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "ab892684",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.        ,  0.63082665,  0.06553787,  0.66237282],\n",
       "       [ 0.        ,  0.        , -0.42374286, -1.69840544],\n",
       "       [ 0.        ,  0.        ,  0.        , -0.03633514],\n",
       "       [ 0.        ,  0.        ,  0.        ,  0.        ]])"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "U"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "9c18e818",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr_A = - np.linalg.inv(D) @ (L + U)\n",
    "vec_b = np.linalg.inv(D) @ b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "fc3e27af",
   "metadata": {},
   "outputs": [],
   "source": [
    "Xold = np.array([1, 1, 1, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "6200dc96",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 [ 2.58194176 -3.33237811 -1.1766828   2.48942098]\n",
      "1 [ 0.81078391 -5.42483929  0.97927188 -0.25051944]\n",
      "2 [-2.31479167  0.4317237  -3.34525283 -7.0173309 ]\n",
      "3 [-3.61888199 15.32820405  4.25911503 -0.37982409]\n",
      "4 [10.57588041 -1.33373827 -2.46920773 14.95045296]\n",
      "5 [ 10.83662222 -33.90684172   5.59101143  14.44229455]\n",
      "6 [ -9.50026345 -29.31947043 -17.20574771 -36.88239304]\n",
      "7 [-43.61255779  85.32973916  13.3058906  -45.39555603]\n",
      "8 [22.52911852 92.51101199 -2.7411278  55.42099834]\n",
      "9 [  97.71620727 -134.8780731    40.16417688  166.74837423]\n",
      "10 [  36.43003963 -360.33013912  -80.71133532  -82.03983455]\n",
      "11 [-283.07214561  221.35566207    9.75644495 -423.48372784]\n",
      "12 [-160.18043017  927.39082941  -12.11150793  -78.21723562]\n",
      "13 [ 521.62822326   76.29714042  285.84159446 1124.77173955]\n",
      "14 [  851.91507946 -2532.50149666  -271.8238844    670.30155155]\n",
      "15 [-1106.74050319 -1239.40625559  -222.29648004 -2320.73605654]\n",
      "16 [-2414.96448732  5342.8556813   -356.41119954 -3198.14004964]\n",
      "17 [1049.8228387  6611.36741759 1703.63499897 4573.01852386]\n",
      "18 [  7390.7296605  -10960.5514836     -88.15647793  10031.4221645 ]\n",
      "19 [   276.64206344 -21369.82392028  -1661.98346774  -5530.81202785]\n"
     ]
    }
   ],
   "source": [
    "for i in range(20):\n",
    "    Xnew = calXnew(Xold, A, b)\n",
    "    Xold = Xnew\n",
    "    print(i, Xnew)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "aafc0705",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1.25438362,  1.46549107,  0.28468474,  0.45146603])"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linalg.inv(A) @ b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "6b228e3c",
   "metadata": {},
   "outputs": [],
   "source": [
    "w = 1.01\n",
    "arr_A = np.linalg.inv(w * L + D) @ ((1 - w) * D - w * U)\n",
    "vec_b = np.linalg.inv(w * L + D) @ (w * b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7dc22e91",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "47a11625",
   "metadata": {},
   "outputs": [],
   "source": [
    "Xold = np.array([1, 1, 1, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "316f0aff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 [ 2.58194176 -3.33237811 -1.1766828   2.48942098]\n",
      "1 [ 0.81078391 -5.42483929  0.97927188 -0.25051944]\n",
      "2 [-2.31479167  0.4317237  -3.34525283 -7.0173309 ]\n",
      "3 [-3.61888199 15.32820405  4.25911503 -0.37982409]\n",
      "4 [10.57588041 -1.33373827 -2.46920773 14.95045296]\n",
      "5 [ 10.83662222 -33.90684172   5.59101143  14.44229455]\n",
      "6 [ -9.50026345 -29.31947043 -17.20574771 -36.88239304]\n",
      "7 [-43.61255779  85.32973916  13.3058906  -45.39555603]\n",
      "8 [22.52911852 92.51101199 -2.7411278  55.42099834]\n",
      "9 [  97.71620727 -134.8780731    40.16417688  166.74837423]\n",
      "10 [  36.43003963 -360.33013912  -80.71133532  -82.03983455]\n",
      "11 [-283.07214561  221.35566207    9.75644495 -423.48372784]\n",
      "12 [-160.18043017  927.39082941  -12.11150793  -78.21723562]\n",
      "13 [ 521.62822326   76.29714042  285.84159446 1124.77173955]\n",
      "14 [  851.91507946 -2532.50149666  -271.8238844    670.30155155]\n",
      "15 [-1106.74050319 -1239.40625559  -222.29648004 -2320.73605654]\n",
      "16 [-2414.96448732  5342.8556813   -356.41119954 -3198.14004964]\n",
      "17 [1049.8228387  6611.36741759 1703.63499897 4573.01852386]\n",
      "18 [  7390.7296605  -10960.5514836     -88.15647793  10031.4221645 ]\n",
      "19 [   276.64206344 -21369.82392028  -1661.98346774  -5530.81202785]\n"
     ]
    }
   ],
   "source": [
    "for i in range(20):\n",
    "    Xnew = calXnew(Xold, arr_A, vec_b)\n",
    "    Xold = Xnew\n",
    "    print(i, Xnew)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "882b0db3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def calStdError(Xnew, Xold):\n",
    "    return np.sqrt((Xnew - Xold) ** 2 / Xnew.size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "78cf64cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Xnew.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "e8e1011d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0.])"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "calStdError(Xnew, Xold)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "adf0fe77",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
